<?php
namespace app\common\model\rebate;
use app\api\controller\Pub;
use app\common\model\user\User;
use app\common\model\power\Rolepower;
use think\Model;

//服务费档案信息
class Rebate extends Model
{
    protected $table = 'rebate';
    protected $pk    = 'RebateId';

    public function userinfo()
    {
        return $this->hasOne('app\common\model\user\User', 'UserId', 'CreateUserId');
    }


    /**
     * 获取列表
     * @param  [type] $id       [服务费记录表id]
     * @param  [type] $usertype [description]
     * @return [type]           [description]
     */
    public static function getlist($id,$usertype){
        $data = Rebate::where('RebateListId',$id)->where('IsDelete',0)->with('userinfo')->visible(['userinfo'=>['Name','Iphone']]);
        switch ($usertype) {
            case 'mana':
                break;
            default:
                $data = $data->hidden(['OperationLog']);
                break;
        }
        $data = $data->select();
        return $data->toArray();
    }

    /**
     * 创建服务费档案列表
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function user_create($idata)
    {   
        $sb = RebateList::get($idata['id']);
        if(empty($sb)){
            return moerr('没找到您需要的服务费记录表');
        }
        $rebate               = new Rebate();
        $rebate->CreateTime   = date('Y-m-d H:i:s', time());
        $rebate->CreateUserId = Pub::userid(); //创建服务费的用户id
        // $rebate->RebateListId = $idata['id'];
        $filed                = ['Img', 'Type', 'Note', 'Limit','Price','RebateListId','RebateTime'];
        foreach ($filed as $key => $value) {
            if (isset($idata[$value])) {
                $rebate->$value = $idata[$value];
            }
        }
        $rebate->save();
        return $rebate->RebateListId;
    }

    /**
     * 判断服务费状态是否可操作
     * @param  [type] $list [id数组]
     * @return [type]       [返回错误代码]
     */
    public static function liststate($id,$hidden=[],$hidloag=true,$jump = true,$type_state=true){
        $data = Rebate::where('RebateId',$id)->find();
        if(empty($data)){
            if($jump){
                return moerr('没有找到您需要的服务费信息');
            }else{
                return [];
            }
        }else{
            if($hidloag==true && Rolepower::ispower(204004) && $data['Type']=='contract'){
                $hidloag = false;
            }
            if($hidloag==true && Rolepower::ispower(204003) && $data['Type']=='hawaorder'){
                $hidloag = false;
            }

            if($hidloag){
                $hidden= array_merge(['OperationLog'],$hidden);
                $data = $data->hidden($hidden)->toArray();
            }else if(empty($hidden)){
                $data = $data->toArray();
            }else{
                $data = $data->hidden($hidden)->toArray();    
            }
            return sok($data);
        }
    }

    /**
     * 获取订单信息
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function getinfo($id,$usertype = 'buss'){
        if($usertype=='mana'){
            $data = self::liststate($id,[],false,true,false);
        }else{
            $data = self::liststate($id,[],true,true,false);
        }
        if (!$data['code']) { return $data; }
        $rdata = $data['msg'];
        $rdata['userinfo'] = User::getpublicinfo($rdata['CreateUserId']);
        return sok($rdata);
    }


    /**
     * 用户修改信息
     * @param  [type]  $data   [description]
     * @param  boolean $wtnote [是否记录]
     * @return [type]          [description]
     */
    public static function user_reinfo($data, $wtnote = true)
    {
        if (empty($data['id'])) {
            return moerr('缺少重要参数');
        }
        $info     = Rebate::get($data['id']);
        $userinfo = User::getinfo();
        $reinfo   = empty($info->OperationLog) ? '' : $info->OperationLog . "\n";
        $reinfo   = $reinfo . '【' . $userinfo->Name . '|' . $userinfo->UserId . '】在' . date('Y-m-d H:i', time()) . '进行了信息调整：';
        $nreinfo  = '';
        // dump($data);
        foreach ($data as $key => $value) {
            $sb = $info->getfiledname($key);
            if (empty($sb)) {
                continue;
            }
            //取原数据
            $ovalue = $info->$key;
            if ($key == 'State' && $value !== $ovalue) {
                $info->$key = $value;
                //value将改变成文字所以提前赋值
                $value   = $info->getStatusText((int) $value);
                $ovalue  = $info->getStatusText((int) $ovalue);
                $nreinfo = $nreinfo . '字段【' . $info->getfiledname($key) . '】由【' . $ovalue . '】修改为【' . $value . '】;' . "\n";
            }  else if ($key == 'Price' && $value !== $ovalue) {
                $info->$key = $value;
                //将差值变更到总表
                $changevalue = bcsub($value,$ovalue);//原先值 减去 新值
                if($changevalue!==0){
                    //将值更新的总表去
                    RebateList::add_price($info->RebateListId,$changevalue);
                }
                $nreinfo = $nreinfo . '字段【' . $info->getfiledname($key) . '】由【' . $ovalue . '】修改为【' . $value . '】;' . "\n";
            }  else if ($key == 'IsDelete' && $value !== $ovalue) {
                $info->$key = $value;
                if($value==1){
                    //将差值变更到总表
                    $changevalue = 0 - $info->Price;//原先值 减去 新值
                }else{
                    $changevalue = $info->Price;//原先值 减去 新值
                }
                //将值更新的总表去
                RebateList::add_price($info->RebateListId,$changevalue);
                
                $nreinfo = $nreinfo . '字段【' . $info->getfiledname($key) . '】由【' . $ovalue . '】修改为【' . $value . '】;' . "\n";
            } else if (isset($value) && $ovalue !== $value) {                
                $nreinfo    = $nreinfo . '字段【' . $info->getfiledname($key) . '】由【' . $ovalue . '】修改为【' . $value . '】;' . "\n";
                $info->$key = $value;
            }
        }
        if (!empty($nreinfo)) {
            if ($wtnote) {
                $reinfo             = $reinfo . "\n" . $nreinfo;
                $info->OperationLog = $reinfo;
            }
            $info->save();
        } else {
            return moerr('没有找到需要调整的数据');
        }
    }

    /**
     * 获取字段的名字，同时仅可删除带名字的字段
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    public static function getfiledname($str)
    {
        $filename = [
            'Note'  => '备注', 'Img'      => '图片备注',
            'State' => '状态', 'IsDelete' => '是否删除', 'Limit' => '服务费比例','Price' =>'合计服务费费用'
        ];
        if (empty($filename[$str])) {
            return false;
        }
        return $filename[$str];
    }

}
